/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cms.dataaccess.hibernate;
import cms.dataaccess.ConferenceDAO;
import cms.exceptions.HibernateAccessLayerException;
import dto.Conference;
import dto.Conferenceitem;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author WU DAO BO 
 */
public class ConferenceDAOImpl extends AbstractDAO implements ConferenceDAO  {
   // private static List<Conference> allConferences  =null;
            
    @Override
    public void saveUpdateConfernce(String name, String eventDetail, String location, boolean callForPaper, Date submitDate, boolean notifyResults, double price) {
       Conference conference = new Conference();
       conference.setConferenceName(name);
       conference.setEventDetail(eventDetail);
       conference.setLocatoin(location); 
       conference.setCallForPaper(callForPaper);
       conference.setSubmiteDate(submitDate);
       conference.setNotifyResults(notifyResults);
       conference.setPrice(price);
        
       this.saveOrUpdate(conference);
        }

    @Override
    public void saveUpdateConfernce(Conference conf) {
        this.saveOrUpdate(conf);
        
    }

    @Override
    public List<Conference> getAllConference() {
       
            return this.findAll(Conference.class);
        
    }

    @Override
    public Conference getConferenceByName(String name) {
      return (Conference) this.findByCustomizeQuery(Conference.class, "conferenceName='"+name+"'").get(0);
    }

    @Override
    public List<Conference> getConfencesByItem(String item) {
         ArrayList<Conference> conResult = new ArrayList<Conference>();
        if (item==null){
           return conResult;
        }
       List<Conferenceitem> itemList = this.findByCustomizeQuery(Conferenceitem.class,
               "itemName='"+item+"'");
       for(int i=0;i<itemList.size();i++){
          Conference  con = this.getConferenceByName(itemList.get(i).getId().getConferenceName());
          conResult.add(con);
         
       }
       return conResult;
    }


    @Override
    public ArrayList<Conference> getConfencesByDate(Date startTime,Date endTime) {
        ArrayList<Conference> conResult = new ArrayList<Conference>();
        if ( startTime==null || endTime==null ){
           return conResult;
        }
       
       
         
           for(int i =0; i< getAllConference().size();i++){
               if(getAllConference().get(i).getSubmiteDate().compareTo(startTime)>=0 && 
                      getAllConference().get(i).getSubmiteDate().compareTo(endTime)<=0) {
                   conResult.add(getAllConference().get(i));
               }
           }
           
           
          return conResult;
    }

    @Override
    public ArrayList<Conference> searchConferncesByEventDetail(String eventDetail) {
       ArrayList<Conference> conResult = new ArrayList<Conference>();
        if (eventDetail==null){
           return conResult;
        }
         
           List<Conference>allConferences=this.getAllConference();
        for(int i=0; i<allConferences.size();i++) {
            if(allConferences.get(i).getEventDetail().contains(eventDetail)){
                conResult.add(allConferences.get(i));
            } 
        }
       return conResult;
    }

    @Override
    public ArrayList<Conference> searchConferncesByPrice(double minPrice, double maxPrice) {
         ArrayList<Conference> conResult = new ArrayList<Conference>();
     
        
      List<Conference> allConferences=this.getAllConference();
       
       
         
       for(int i =0; i< allConferences.size();i++){
               if(allConferences.get(i).getPrice()>= minPrice&& 
                      allConferences.get(i).getPrice()<= maxPrice) {
                   conResult.add(allConferences.get(i));
               }
       }
           
           
          return conResult;
    }

    @Override
    public void deleteConference(Conference conf) {
     this.delete(conf);
    }

    @Override
    public List<Conference> findByCustomizeQuery(String queryString) throws HibernateAccessLayerException {
          return (List<Conference>) (this.findByCustomizeQuery(Conference.class, queryString));
         
    }

  
}
      
    
